const { Service } = require('egg');
const ERROR_CODE = require('../errorCode');
const {
  Course,
  Student,
  ExperimentTotalScore,
} = require('../db/model');

class ExperimentTotalScoreService extends Service {
  async getAllExperimentTotalScore() {
    await Course.hasMany(ExperimentTotalScore);
    await ExperimentTotalScore.belongsTo(Course, {
      foreignKey: 'cId',
    });
    await Student.hasMany(ExperimentTotalScore);
    await ExperimentTotalScore.belongsTo(Student, {
      foreignKey: 'sId',
    });
    const res = await ExperimentTotalScore.findAll({
      attributes: { exclude: ['id', 'CourseCId', 'StudentSId'] },
      include: [ Student, Course ],
    }).catch(err => {
      console.log(`ExperimentTotalScore 表查找失败: ${err}`);
    });
    if (res === undefined) {
      console.log(`ExperimentTotalScore 表查找失败: ${ERROR_CODE.DATABASE_ERROR}`);
      return ERROR_CODE.DATABASE_ERROR;
    }
    return res;
  }

  async getExperimentTotalScore(experimentTotalScore) {
    if (!(experimentTotalScore?.sId && experimentTotalScore?.cId)) {
      console.log(`ExperimentTotalScore 表查找失败: ${ERROR_CODE.PARAMETERS_MISSING}\n参数: ${experimentTotalScore}`, experimentTotalScore);
      return ERROR_CODE.PARAMETERS_MISSING;
    }
    await Course.hasMany(ExperimentTotalScore);
    await ExperimentTotalScore.belongsTo(Course, {
      foreignKey: 'cId',
    });
    await Student.hasMany(ExperimentTotalScore);
    await ExperimentTotalScore.belongsTo(Student, {
      foreignKey: 'sId',
    });
    const res = await ExperimentTotalScore.findOne({
      where: {
        sId: experimentTotalScore?.sId || -1,
        cId: experimentTotalScore?.cId || -1,
      },
      attributes: { exclude: ['id', 'CourseCId', 'StudentSId'] },
      include: [ Student, Course ],
    }).catch(err => {
      console.log(`ExperimentTotalScore 表查找失败: ${err}\n参数: ${experimentTotalScore}`, experimentTotalScore);
    });
    if (res === undefined) {
      console.log(`ExperimentTotalScore 表查找失败: ${ERROR_CODE.DATABASE_ERROR}\n参数: ${experimentTotalScore}`, experimentTotalScore);
      return ERROR_CODE.DATABASE_ERROR;
    }
    if (res === null) {
      console.log(`ExperimentTotalScore 表查找失败: ${ERROR_CODE.PRIMARY_KEY_NOT_EXIST}\n参数: ${experimentTotalScore}`, experimentTotalScore);
      return ERROR_CODE.PRIMARY_KEY_NOT_EXIST;
    }
    return res;
  }

  async appendExperimentTotalScore(experimentTotalScore) {
    let res = await this.getExperimentTotalScore(experimentTotalScore);
    if (res instanceof Object) {
      res = ERROR_CODE.PRIMARY_KEY_EXIST;
    }
    if (res !== ERROR_CODE.PRIMARY_KEY_NOT_EXIST) {
      console.log(`ExperimentTotalScore 表新增失败: ${res}\n参数: ${experimentTotalScore}`, experimentTotalScore);
      return res;
    }
    res = await ExperimentTotalScore.create(experimentTotalScore).catch(err => {
      console.log(`ExperimentTotalScore 表新增失败: ${err}\n参数: ${experimentTotalScore}`, experimentTotalScore);
    });
    if (res === undefined) {
      console.log(`ExperimentTotalScore 表新增失败: ${ERROR_CODE.DATABASE_ERROR}\n参数: ${experimentTotalScore}`, experimentTotalScore);
      return ERROR_CODE.DATABASE_ERROR;
    }
    return res;
  }

  async updateExperimentTotalScore(experimentTotalScore) {
    let res = await this.getExperimentTotalScore(experimentTotalScore);
    if (Object.values(ERROR_CODE).includes(res)) {
      console.log(`ExperimentTotalScore 表更新失败: ${res}\n参数: ${experimentTotalScore}`, experimentTotalScore);
      return res;
    }
    res = await ExperimentTotalScore.update(experimentTotalScore, {
      where: {
        sId: experimentTotalScore?.sId || -1,
        cId: experimentTotalScore?.cId || -1,
      },
    }).catch(err => {
      console.log(`ExperimentTotalScore 表更新失败: ${err}\n参数: ${experimentTotalScore}`, experimentTotalScore);
    });
    if (res === undefined) {
      console.log(`ExperimentTotalScore 表更新失败: ${ERROR_CODE.DATABASE_ERROR}\n参数: ${experimentTotalScore}`, experimentTotalScore);
      return ERROR_CODE.DATABASE_ERROR;
    }
    return res;
  }

  async destroyExperimentTotalScore(experimentTotalScore) {
    let res = await this.getExperimentTotalScore(experimentTotalScore);
    if (Object.values(ERROR_CODE).includes(res)) {
      console.log(`ExperimentTotalScore 表删除失败: ${res}\n参数: ${experimentTotalScore}`, experimentTotalScore);
      return res;
    }
    res = await ExperimentTotalScore.destroy({
      where: {
        sId: experimentTotalScore?.sId || -1,
        cId: experimentTotalScore?.cId || -1,
      },
    }).catch(err => {
      console.log(`ExperimentTotalScore 表删除失败: ${err}\n参数: ${experimentTotalScore}`, experimentTotalScore);
    });
    if (res === undefined) {
      console.log(`ExperimentTotalScore 表删除失败: ${ERROR_CODE.DATABASE_ERROR}\n参数: ${experimentTotalScore}`, experimentTotalScore);
      return ERROR_CODE.DATABASE_ERROR;
    }
    return res;
  }
}

module.exports = ExperimentTotalScoreService;
